package com.aiyige.page.my.message.notifymessage.interactmessage.model;

import com.aiyige.R;
import com.aiyige.model.message.entity.Message;
import com.aiyige.model.message.view.CommentContent;
import com.aiyige.model.message.view.FavoriteContent;
import com.aiyige.model.message.view.OrderContent;
import com.aiyige.model.message.view.PraiseContent;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.page.my.message.notifymessage.commentmessage.model.CommentMessage;
import com.aiyige.page.my.message.util.MessageUtil;
import com.aiyige.utils.StringUtils;

public class InteractMessage {
    int type;
    String otherId;
    String goodsId;
    String otherName;
    String otherAvatar;
    String cover;
    long otherTime;
    String mediaTitle;
    long mediaReleaseDate;
    int targetType;
    String router;
    String goodsType;
    String otherCommentId;
    String otherComment;
    String myAvatar;
    String myComment;
    String myName;
    String orderNo;
    String contact;
    String otherOrderMessage;
    double price;
    int quantity;
    String refundRouter;
    int orderStatus;
    String rejectReason;
    String orderId;

    public InteractMessage() {
    }

    private InteractMessage(Builder builder) {
        setType(builder.type);
        setOtherId(builder.otherId);
        setGoodsId(builder.goodsId);
        setOtherName(builder.otherName);
        setOtherAvatar(builder.otherAvatar);
        setCover(builder.cover);
        setOtherTime(builder.otherTime);
        setMediaTitle(builder.mediaTitle);
        setMediaReleaseDate(builder.mediaReleaseDate);
        setTargetType(builder.targetType);
        setRouter(builder.router);
        setGoodsType(builder.goodsType);
        setOtherCommentId(builder.otherCommentId);
        setOtherComment(builder.otherComment);
        setMyAvatar(builder.myAvatar);
        setMyComment(builder.myComment);
        setMyName(builder.myName);
        setOrderNo(builder.orderNo);
        setContact(builder.contact);
        setOtherOrderMessage(builder.otherOrderMessage);
        setPrice(builder.price);
        setQuantity(builder.quantity);
        setRefundRouter(builder.refundRouter);
        setOrderStatus(builder.orderStatus);
        setRejectReason(builder.rejectReason);
        setOrderId(builder.orderId);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getOtherId() {
        return otherId;
    }

    public void setOtherId(String otherId) {
        this.otherId = otherId;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getOtherName() {
        return otherName;
    }

    public void setOtherName(String otherName) {
        this.otherName = otherName;
    }

    public String getOtherAvatar() {
        return otherAvatar;
    }

    public void setOtherAvatar(String otherAvatar) {
        this.otherAvatar = otherAvatar;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public long getOtherTime() {
        return otherTime;
    }

    public void setOtherTime(long otherTime) {
        this.otherTime = otherTime;
    }

    public String getMediaTitle() {
        return mediaTitle;
    }

    public void setMediaTitle(String mediaTitle) {
        this.mediaTitle = mediaTitle;
    }

    public long getMediaReleaseDate() {
        return mediaReleaseDate;
    }

    public void setMediaReleaseDate(long mediaReleaseDate) {
        this.mediaReleaseDate = mediaReleaseDate;
    }

    public int getTargetType() {
        return targetType;
    }

    public void setTargetType(int targetType) {
        this.targetType = targetType;
    }

    public String getRouter() {
        return router;
    }

    public void setRouter(String router) {
        this.router = router;
    }

    public String getGoodsType() {
        return goodsType;
    }

    public void setGoodsType(String goodsType) {
        this.goodsType = goodsType;
    }

    public String getOtherCommentId() {
        return otherCommentId;
    }

    public void setOtherCommentId(String otherCommentId) {
        this.otherCommentId = otherCommentId;
    }

    public String getOtherComment() {
        return otherComment;
    }

    public void setOtherComment(String otherComment) {
        this.otherComment = otherComment;
    }

    public String getMyAvatar() {
        return myAvatar;
    }

    public void setMyAvatar(String myAvatar) {
        this.myAvatar = myAvatar;
    }

    public String getMyComment() {
        return myComment;
    }

    public void setMyComment(String myComment) {
        this.myComment = myComment;
    }

    public String getMyName() {
        return myName;
    }

    public void setMyName(String myName) {
        this.myName = myName;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getOtherOrderMessage() {
        return otherOrderMessage;
    }

    public void setOtherOrderMessage(String otherOrderMessage) {
        this.otherOrderMessage = otherOrderMessage;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getRefundRouter() {
        return refundRouter;
    }

    public void setRefundRouter(String refundRouter) {
        this.refundRouter = refundRouter;
    }

    public int getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(int orderStatus) {
        this.orderStatus = orderStatus;
    }

    public String getRejectReason() {
        return rejectReason;
    }

    public void setRejectReason(String rejectReason) {
        this.rejectReason = rejectReason;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }


    public static final class Builder {
        private int type;
        private String otherId;
        private String goodsId;
        private String otherName;
        private String otherAvatar;
        private String cover;
        private long otherTime;
        private String mediaTitle;
        private long mediaReleaseDate;
        private int targetType;
        private String router;
        private String goodsType;
        private String otherCommentId;
        private String otherComment;
        private String myAvatar;
        private String myComment;
        private String myName;
        private String orderNo;
        private String contact;
        private String otherOrderMessage;
        private double price;
        private int quantity;
        private String refundRouter;
        private int orderStatus;
        private String rejectReason;
        private String orderId;

        private Builder() {
        }

        public Builder type(int val) {
            type = val;
            return this;
        }

        public Builder otherId(String val) {
            otherId = val;
            return this;
        }

        public Builder goodsId(String val) {
            goodsId = val;
            return this;
        }

        public Builder otherName(String val) {
            otherName = val;
            return this;
        }

        public Builder otherAvatar(String val) {
            otherAvatar = val;
            return this;
        }

        public Builder cover(String val) {
            cover = val;
            return this;
        }

        public Builder otherTime(long val) {
            otherTime = val;
            return this;
        }

        public Builder mediaTitle(String val) {
            mediaTitle = val;
            return this;
        }

        public Builder mediaReleaseDate(long val) {
            mediaReleaseDate = val;
            return this;
        }

        public Builder targetType(int val) {
            targetType = val;
            return this;
        }

        public Builder router(String val) {
            router = val;
            return this;
        }

        public Builder goodsType(String val) {
            goodsType = val;
            return this;
        }

        public Builder otherCommentId(String val) {
            otherCommentId = val;
            return this;
        }

        public Builder otherComment(String val) {
            otherComment = val;
            return this;
        }

        public Builder myAvatar(String val) {
            myAvatar = val;
            return this;
        }

        public Builder myComment(String val) {
            myComment = val;
            return this;
        }

        public Builder myName(String val) {
            myName = val;
            return this;
        }

        public Builder orderNo(String val) {
            orderNo = val;
            return this;
        }

        public Builder contact(String val) {
            contact = val;
            return this;
        }

        public Builder otherOrderMessage(String val) {
            otherOrderMessage = val;
            return this;
        }

        public Builder price(double val) {
            price = val;
            return this;
        }

        public Builder quantity(int val) {
            quantity = val;
            return this;
        }

        public Builder refundRouter(String val) {
            refundRouter = val;
            return this;
        }

        public Builder orderStatus(int val) {
            orderStatus = val;
            return this;
        }

        public Builder rejectReason(String val) {
            rejectReason = val;
            return this;
        }

        public Builder orderId(String val) {
            orderId = val;
            return this;
        }

        public InteractMessage build() {
            return new InteractMessage(this);
        }
    }


    public static InteractMessage parse(Message message) {
        try {
            switch (message.getType()) {
                case Message.TYPE_COMMENT: {
                    CommentContent record = message.getContent().toJavaObject(CommentContent.class);
                    int targetType;
                    if (record.getComment().getOriginComment() != null) {
                        targetType = MessageUtil.TARGET_TYPE_COMMENT;
                    } else {
                        targetType = MessageUtil.convertMomentSubjectToTargetType(record.getMoment().getSubject());
                    }
                    String myComment = "";
                    try {
                        myComment = record.getComment().getOriginComment().getContent();
                    } catch (Exception e) {

                    }
                    String myName = "";
                    try {
                        myName = record.getComment().getOriginComment().getUser();
                    } catch (Exception e) {

                    }
                    return InteractMessage.newBuilder()
                            .type(MessageUtil.MESSAGE_TYPE_COMMENT)
                            .otherAvatar(record.getSender().getAvatar())
                            .otherId(record.getSender().getId())
                            .otherName(record.getSender().getName())
                            .goodsId(record.getMoment().getId())
                            .goodsType(record.getMoment().getSubject())
                            .otherTime(message.getCreateTime())
                            .mediaReleaseDate(record.getMoment().getCreateTime())
                            .cover(record.getMoment().getCover())
                            .targetType(targetType)
                            .mediaTitle(record.getMoment().getTitle())
                            .myComment(myComment)
                            .myName(myName)
                            .router(record.getMoment().getRouter())
                            .otherComment(record.getComment().getContent())
                            .otherCommentId(record.getComment().getId())
                            .build();
                }
                case Message.TYPE_FAVOURITE: {
                    FavoriteContent record = message.getContent().toJavaObject(FavoriteContent.class);
                    int targetType = MessageUtil.convertMomentSubjectToTargetType(record.getMoment().getSubject());
                    return InteractMessage.newBuilder()
                            .type(MessageUtil.MESSAGE_TYPE_COLLECTION)
                            .otherAvatar(record.getSender().getAvatar())
                            .otherId(record.getSender().getId())
                            .otherName(record.getSender().getName())
                            .goodsId(record.getMoment().getId())
                            .otherTime(message.getCreateTime())
                            .mediaReleaseDate(record.getMoment().getCreateTime())
                            .cover(record.getMoment().getCover())
                            .targetType(targetType)
                            .router(record.getMoment().getRouter())
                            .mediaTitle(record.getMoment().getTitle())
                            .build();
                }
                case Message.TYPE_ORDER: {
                    OrderContent record = message.getContent().toJavaObject(OrderContent.class);
                    int targetType = MessageUtil.convertMomentSubjectToTargetType(record.getMoment().getSubject());
                    int orderStatus = 0;
                    String otherOrderMessage = "";
                    switch (record.getStatus()) {
                        case OrderContent.ORDER_STATUS_UNPAY:
                            otherOrderMessage = StringUtils.getString(R.string.order_message_status_nopay);
                            orderStatus = OrderContent.ORDER_STATUS_UNPAY;
                            break;
                        case OrderContent.ORDER_STATUS_PAYED:
                            otherOrderMessage = StringUtils.getString(R.string.order_message_status_payed);
                            orderStatus = OrderContent.ORDER_STATUS_PAYED;
                            break;
                        case OrderContent.ORDER_STATUS_CONFIRM:
                            otherOrderMessage = StringUtils.getString(R.string.order_message_status_confirmed, StringUtils.priceFormat(record.getAmount()));
                            orderStatus = OrderContent.ORDER_STATUS_CONFIRM;
                            break;
                        case OrderContent.ORDER_STATUS_REFUNDED:
                            otherOrderMessage = StringUtils.getString(R.string.order_message_status_refunded);
                            orderStatus = OrderContent.ORDER_STATUS_REFUNDED;
                            break;
                        case OrderContent.ORDER_STATUS_REFUNDING:
                            otherOrderMessage = StringUtils.getString(R.string.order_message_status_refunding);
                            orderStatus = OrderContent.ORDER_STATUS_REFUNDING;
                            break;
                        case OrderContent.ORDER_STATUS_REJECT_REFUND:
                            otherOrderMessage = StringUtils.getString(R.string.order_message_status_reject_refund);
                            orderStatus = OrderContent.ORDER_STATUS_REJECT_REFUND;
                            break;
                    }

                    return InteractMessage.newBuilder()
                            .type(MessageUtil.MESSAGE_TYPE_ORDER)
                            .otherAvatar(record.getSender().getAvatar())
                            .otherId(record.getSender().getId())
                            .otherName(record.getSender().getName())
                            .goodsId(record.getMoment().getId())
                            .otherTime(message.getCreateTime())
                            .mediaReleaseDate(record.getMoment().getCreateTime())
                            .cover(record.getMoment().getCover())
                            .targetType(targetType)
                            .mediaTitle(record.getMoment().getTitle())
                            .orderNo(record.getCode())
                            .orderId(record.getId())
                            .refundRouter(record.getRefundRouter())
                            .router(record.getRouter())
                            .otherOrderMessage(otherOrderMessage)
                            .orderStatus(orderStatus)
                            .rejectReason(record.getRefundRejectReason())
                            .build();
                }
                case Message.TYPE_PRAISE: {
                    PraiseContent record = message.getContent().toJavaObject(PraiseContent.class);
                    int targetType;
                    if (record.getComment() != null) {
                        targetType = MessageUtil.TARGET_TYPE_COMMENT;
                    } else {
                        targetType = MessageUtil.convertMomentSubjectToTargetType(record.getMoment().getSubject());
                    }

                    String myComment = "";
                    try {
                        myComment = record.getComment().getContent();
                    } catch (Exception e) {

                    }
                    String myName = "";
                    try {
                        myName = record.getComment().getUser();
                    } catch (Exception e) {

                    }
                    return InteractMessage.newBuilder()
                            .type(MessageUtil.MESSAGE_TYPE_LIKE)
                            .otherAvatar(record.getSender().getAvatar())
                            .otherId(record.getSender().getId())
                            .otherName(record.getSender().getName())
                            .goodsId(record.getMoment().getId())
                            .otherTime(message.getCreateTime())
                            .mediaReleaseDate(record.getMoment().getCreateTime())
                            .cover(record.getMoment().getCover())
                            .targetType(targetType)
                            .mediaTitle(record.getMoment().getTitle())
                            .myComment(myComment)
                            .myName(myName)
                            .router(record.getMoment().getRouter())
                            .build();
                }
                default: {
                    return null;
                }
            }
        } catch (Exception e) {
            return null;
        }
    }


}
